//
//  UserAuthViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/24.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class UserAuthViewModel {
    
    public static let shared = UserAuthViewModel()
    
    private var provider: RxMoyaProvider<UserAuthApi>
    
    private init() {
        let endpointClosure = { (target: UserAuthApi) -> Endpoint<UserAuthApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json"])
        }
        provider = RxMoyaProvider<UserAuthApi>(endpointClosure: endpointClosure)
    }
    
    func applyForRegisterEmail(email: String) -> Observable<Any> {
        return provider.request(.applyForRegisterEmail(email: email))
            .mapJSON()
    }
    
    func getChangePWTotp(email: String) -> Observable<Any> {
        return provider.request(.getForgetPWTotp(email: email))
            .mapJSON()
    }
    
    func verifyTOTP(email: String, totp: String) -> Observable<TOTPAuthResult> {
        return provider.request(.totpVerification(email: email, totp: totp))
            .mapJSON()
            .mapObject(type: TOTPAuthResult.self)
    }
    
    func changePW(newPW: String, token: String) -> Observable<ChangePWResponse> {
        let endpointClosure = { (target: UserAuthApi) -> Endpoint<UserAuthApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        let provider = RxMoyaProvider<UserAuthApi>(endpointClosure: endpointClosure)
        return provider.request(.changePW(newPW: newPW))
            .mapJSON()
            .mapObject(type: ChangePWResponse.self)
    }
    
    func register(username: String, password: String, token: String) -> Observable<UserRegisterResponse> {
        let endpointClosure = { (target: UserAuthApi) -> Endpoint<UserAuthApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        let provider = RxMoyaProvider<UserAuthApi>(endpointClosure: endpointClosure)
        return provider.request(.userRegister(username: username, password: password))
            .mapJSON()
            .mapObject(type: UserRegisterResponse.self)
    }
    
    func login(username: String, password: String) -> Observable<UserTokenResponse> {
        return provider.request(.getToken(username: username, password: password))
            .mapJSON()
            .mapObject(type: UserTokenResponse.self)
    }
    
    func getTotp(token: String) -> Observable<String> {
        let endpointClosure = { (target: UserAuthApi) -> Endpoint<UserAuthApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        let provider = RxMoyaProvider<UserAuthApi>(endpointClosure: endpointClosure)
        return provider.request(.getTotp())
               .mapString()
    }
}
